﻿using System;

using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;

namespace ScriptXCompiler
{
    public class IssueLocation
    {
        public static IssueLocation NA = new IssueLocation();

        private IssueLocation()
        {
        }

        public IssueLocation(SyntaxTree tree, SyntaxNode node)
        {
            this._fileName = tree.FilePath;
            InitPosition(tree, node.Span);
        }

        public IssueLocation(SyntaxTree tree, SyntaxToken token)
        {
            this._fileName = tree.FilePath;
            InitPosition(tree, token.Span);
        }

        private void InitPosition(SyntaxTree tree, TextSpan span)
        {
            var x = tree.GetLineSpan(span, false);
            this._line = x.StartLinePosition.Line + 1;
            this._endLine = x.EndLinePosition.Line;
            _column = x.StartLinePosition.Character + 1;
            _endColumn = x.EndLinePosition.Character;
        }

        private string _fileName;
        public string FileName
        {
            get { return _fileName; }
        }

        private int _line;
        public int Line
        {
            get { return _line; }
        }

        private int _column;
        public int Column
        {
            get { return _column; }
        }

        private int _endLine;
        public int EndLine
        {
            get { return _endLine; }
        }

        private int _endColumn;
        public int EndColumn
        {
            get { return _endColumn; }
        }

        public override string ToString()
        {
            return string.Format("in file {0}, line {1}, column {2}", this.FileName, this.Line, this.Column);
        }
    }
}
